package com.wanxi.udp.demo4广播;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * 广播消息发送端
 */
public class UDPBroadcastSend {
    public static void main(String[] args) throws IOException {
        System.out.println("广播发送端启动");
        //1 创建发送端socket
        DatagramSocket ds = new DatagramSocket();
        String s = "广播aaa hello";
        byte[] bytes = s.getBytes();
        // 这里和单播唯一不同的是地址不同。
        InetAddress address = InetAddress.getByName("255.255.255.255");
        int port = 10001;
        // 创建发送的数据包
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
        //发送数据
        ds.send(dp);

        ds.close();
    }
}
